<?php

require_once SITE_ROOT_PATH."/_lib/classes/class.hotel_instances_you.php";
require_once SITE_ROOT_PATH."/_lib/classes/class.hotel_instances_mcs.php";
require_once SITE_ROOT_PATH."/_lib/classes/class.tree.php";
require_once SITE_ROOT_PATH."/_lib/classes/class.holidays.php";

class hotels extends tree {
	
	
	


// FUNCTION // find hotels
function search_hotels($params){


	$params = in($params);

	if(!empty($params['prefix'])) $where .= " AND UPPER(LEFT(name, 1)) = '".$params['prefix']."' ";
	if(!empty($params['name'])) $where .= " AND name LIKE '%".$params['name']."%' ";
	if(!empty($params['parent_id'])) $where .= " AND FIND_IN_SET('".$params['parent_id']."', CONCAT(lineage_base,',',tree_id)) ";
	if(!empty($params['desc_text'])) $where .= " AND description LIKE '%".$params['desc_text']."%' ";
	
	switch($params['order']){
		default: case "name":
			$sql_order = " ORDER by name "; 
		break;
		case "stars":
			$sql_order = " ORDER by hotel_rating, name "; 
		break;
		case "tree":
			$sql_order = " ORDER by tree_id "; 
		break;
		case "updated":
			$sql_order = " ORDER by last_updated "; 
		break;
	}

    $sql = "SELECT SUBSTRING_INDEX(lineage_base, ',', -1) AS parent_id, CONCAT(lineage_base,',',tree_id) AS lineage, tree_id, type_id, lineage_base, name, description, main_photo, hotel_rating, last_updated
    FROM tree
    WHERE 1 = 1 
	AND type_id = '3'
	".$where."
    ".$sql_order." "; 
	

	return apply_query($sql, $num_rows, $params['cache']);
	


}



// FUNCTION // see if a linked instance exists and return hotel values if found
function lookup_hotel($params){


	if($params['tree_id']){
		
		// Get from branch
		$params_get['tree_id'] = $params['tree_id'];
		$params_get['cache'] = $params['cache'];
		$hotel_values = $this->get_branch($params_get);
		$hotel_values = $hotel_values[0];
		unset($params_get);

	}elseif($params['supplier_code']){

		// Get from supplier lookup
		switch($params['supplier_code']){
			case "you":
				$hotel_instances_you = new hotel_instances_you();
				$hotel_values = $hotel_instances_you->lookup_hotel($params);
				$hotel_values = $hotel_values[0];
			break;
			case "mcs":
				$hotel_instances_mcs = new hotel_instances_mcs();
				$hotel_values = $hotel_instances_mcs->lookup_hotel($params);
				$hotel_values = $hotel_values[0];
			break;
		}
	
		if(!$hotel_values){
			// Flag instance
			$flag = true;
			// Dont flag ski to's
			$holidays = new holidays();
			if(in_array($holiday_values['tour_op'],$holidays->ski_only_tos)) $flag = false;
			// Dont flag cruises
			if(strstr(strtolower($holiday_values['resort']),'cruise')) $flag = false;
			// ASYNC
			if($flag==true){
				$hotel_instances_mcs = new hotel_instances_mcs();
				$hotel_instances_mcs->flag_not_found($params);
				$hotel_found = false;
			}
		}
				
	}
	
	

	if($hotel_values['type_id']==3){
				
		// Get resort
		$params_get['tree_id'] = $hotel_values['parent_id'];
		$resort_values = $this->get_branch($params_get);
		$resort_values = $resort_values[0];
		unset($params_get);
		
		//Get destination
		$params_get['tree_id'] = $resort_values['parent_id'];
		$destination_values = $this->get_branch($params_get);
		$destination_values = $destination_values[0];
		unset($params_get);
		
		
	}elseif($hotel_values['type_id']==4){
		
		// Set the resort name
		$resort_values['name'] = $params['resort'];
		// Get destinaion usig the airport code paramater
		$params['airport_code'] = $params['destination_airport'];
		$params['type_id'] = '1';
		$params['position'] = 'bottom';
		$destination_values = $this->destination_lookup($params);
		$destination_values = $destination_values[0];
			
	}else{
		
		// Build details from params
		
		// Get hotel name
		$hotel_values['name'] = $params['hotel_name'];
		// Set the resort name
		$resort_values['name'] = $params['resort'];
		// Get destinaion usig the airport code paramater
		$params['airport_code'] = $params['destination_airport'];
		$params['type_id'] = '1';
		$params['position'] = 'bottom';
		$destination_values = $this->destination_lookup($params);
		$destination_values = $destination_values[0];
			
	}
	
	
	return array('hotel_values'=>$hotel_values, 'resort_values'=>$resort_values, 'destination_values'=>$destination_values);
	
}





// FUNCTION // Insert hotel instance into the flagged table
function flag_hotel_instance($params){
	

	if(!empty($params['supplier_code'])){
	
		switch($params['supplier_code']){
			case "you":
        		$hotel_instances_you = new hotel_instances_you();
				// Search to see if the hotel instance has been flagged before
        		$flagged = $hotel_instances_you->lookup_flagged_instance($params);
				if(!$flagged){
					$hotel_instances_you->flag_not_found($params);
					//echo "need to flag";
				}else{
					//echo "already in there";
				}
			break;
			case "mcs":
        		$hotel_instances_mcs = new hotel_instances_mcs();
				// Search to see if the hotel instance has been flagged before
        		$flagged = $hotel_instances_mcs->lookup_flagged_instance($params);
				if(!$flagged) $hotel_instances_mcs->flag_not_found($params);
			break;
		}

	}else{
	
		return false;
		
	}

}





// FUNCTION // validate the data for the insert/edit hotel details
function validate_details_form($params){

    $my_array = array();
        
    if(!$params['name']) array_push($my_array, "Please enter the name of the hotels");
    if(!$params['description']) array_push($my_array, "Please enter the description");
    if(!$params['parent_id']) array_push($my_array, "Please enter the parent resort");
    
    
    return $my_array;

}





}

?>